package com.sztx.pay.center.worker.quartz;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.sztx.pay.center.common.util.ConfigUtil;
import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.DateUtil;
import com.sztx.pay.center.dataaccess.dao.mysql.DumpRecordMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderB2cMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderRechargeMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderRefundMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderTransferMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderWithdrawMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.PayFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.SettleOrderMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.DumpRecordDO;
import com.sztx.se.core.quartz.job.BaseJob;

@Component("dumpRecordJob")
public class DumpRecordJob extends BaseJob {

	@Autowired
	private DumpRecordMysqlDAO dumpRecordMysqlDAO;
	@Autowired
	private OrderB2cMysqlDAO orderB2cMysqlDAO;
	@Autowired
	private OrderTransferMysqlDAO orderTransferMysqlDAO;
	@Autowired
	private OrderWithdrawMysqlDAO orderWithdrawMysqlDAO;
	@Autowired
	private OrderRechargeMysqlDAO orderRechargeMysqlDAO;
	@Autowired
	private OrderRefundMysqlDAO orderRefundMysqlDAO;
	@Autowired
	private UserActFlowMysqlDAO userActFlowMysqlDAO;
	@Autowired
	private PayFlowMysqlDAO payFlowMysqlDAO;
	@Autowired
	private SettleOrderMysqlDAO settleOrderMysqlDAO;

	protected final Logger LOG = LoggerFactory.getLogger(DumpRecordJob.class);

	@Override
	public Object executeJob(JobExecutionContext arg0) throws JobExecutionException {
		
		log.info("开始数据转储记录");
		int beforeDay = ConfigUtil.dumpBeforeDay;

		Date beforeDate = getBeforeDate(new Date(), beforeDay);
		DumpRecordDO dumpRecordDO = new DumpRecordDO();
		dumpRecordDO.setDay(beforeDate);

		List<DumpRecordDO> dumpRecordDOs = dumpRecordMysqlDAO.listPage(dumpRecordDO, null);

		if (CollectionUtils.isEmpty(dumpRecordDOs)) {
			List<DumpRecordDO> batchSaveDumpRecords = new ArrayList<DumpRecordDO>();
			batchSaveDumpRecords.add(createB2cDumpRecord(beforeDate));
			batchSaveDumpRecords.add(createTransferDumpRecord(beforeDate));
			batchSaveDumpRecords.add(createActFlowDumpRecord(beforeDate));
			batchSaveDumpRecords.add(createRefundDumpRecord(beforeDate));
			batchSaveDumpRecords.add(createRechargeDumpRecord(beforeDate));
			batchSaveDumpRecords.add(createWithdrawDumpRecord(beforeDate));
			dumpRecordMysqlDAO.batchSave(batchSaveDumpRecords);
		}
		
		//TODO 查询前一天的转储数据,进行删除(暂时关闭)
		/**
		Date beforeDate2 = getBeforeDate(new Date(), beforeDay + 1);
		 
		DumpRecordDO berforeDumpRecord = new DumpRecordDO();
		berforeDumpRecord.setDay(beforeDate2);
		List<DumpRecordDO> dumpRecordDO2s = dumpRecordMysqlDAO.listPage(berforeDumpRecord, null);
		
		boolean deleteFlag = true;
		
		if (CollectionUtils.isEmpty(dumpRecordDO2s)) {
			deleteFlag = false;
		} else {
			for (DumpRecordDO dumpRecordDO2 : dumpRecordDO2s) {
				Long totalCount = dumpRecordDO2.getTotalCount();
				Long failedCount = dumpRecordDO2.getFailedCount();
				Long succeedCount = dumpRecordDO2.getSucceedCount();
				if (totalCount != (succeedCount + failedCount) || dumpRecordDO2.getDeleteState() != 0) {
					deleteFlag = false;
					break;
				}
			}
		}
		
		if (deleteFlag) {
			//更新dumpRecord表的删除状态为删除中(0-->1)-->进行删除操作-->更新dumpRecord表的删除状态为删除完成(1-->2)
			String startTime = DateUtil.formatDate(beforeDate2, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
			String endTime = DateUtil.formatDate(beforeDate2, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";
			String day = DateUtil.formatDate(beforeDate2, DateUtil.SHORT_DATE_FORMAT_STR);
			log.info("开始删除已经迁移了的数据");
			//删除b2c订单
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.B2C.getCode(), day, 1);
			orderB2cMysqlDAO.deleteDumpCount(startTime, endTime);
			settleOrderMysqlDAO.deleteDumpCount(startTime, endTime);
			payFlowMysqlDAO.deleteDumpCount(startTime, endTime);
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.B2C.getCode(), day, 2);
			//删除充值订单
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.CHARGE.getCode(), day, 1);
			orderRechargeMysqlDAO.deleteDumpCount(startTime, endTime);
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.CHARGE.getCode(), day, 2);
			//删除退款订单
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.REFUND.getCode(), day, 1);
			orderRefundMysqlDAO.deleteDumpCount(startTime, endTime);
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.REFUND.getCode(), day, 2);
			//删除转账订单
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.TRANSFER.getCode(), day, 1);
			orderTransferMysqlDAO.deleteDumpCount(startTime, endTime);
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.TRANSFER.getCode(), day, 2);
			//删除提现订单
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.WITHDRAW.getCode(), day, 1);
			orderWithdrawMysqlDAO.deleteDumpCount(startTime, endTime);
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.WITHDRAW.getCode(), day, 2);
			//删除资金流水
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.TRADEFLOW.getCode(), day, 1);
			userActFlowMysqlDAO.deleteDumpCount(startTime, endTime);
			dumpRecordMysqlDAO.updateDeleteState(Constance.DumpRecordType.TRADEFLOW.getCode(), day, 2);
		}
		*/
		log.info("结束数据转储记录");
		return null;
	}

	private DumpRecordDO createActFlowDumpRecord(Date date) {
		
//		String startTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
//		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";
//		
//		Integer dumpCount = payFlowMysqlDAO.dumpCount(startTime, endTime);
		
		return createDumpRecord(0, Constance.DumpRecordType.TRADEFLOW, date);
	}
	
	private DumpRecordDO createRefundDumpRecord(Date date) {
		
//		String startTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
//		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";
//		
//		Integer dumpCount = orderRefundMysqlDAO.dumpCount(startTime, endTime);
		
		return createDumpRecord(0, Constance.DumpRecordType.REFUND, date);
	}
	
	private DumpRecordDO createRechargeDumpRecord(Date date) {
		
//		String startTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
//		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";
//		
//		Integer dumpCount = orderRechargeMysqlDAO.dumpCount(startTime, endTime);
		
		return createDumpRecord(0, Constance.DumpRecordType.CHARGE, date);
	}
	
	private DumpRecordDO createWithdrawDumpRecord(Date date) {
		
//		String startTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
//		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";
//		
//		Integer dumpCount = orderWithdrawMysqlDAO.dumpCount(startTime, endTime);
		
		return createDumpRecord(0, Constance.DumpRecordType.WITHDRAW, date);
	}
	
	private DumpRecordDO createTransferDumpRecord(Date date) {
		
//		String startTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
//		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";
//		
//		Integer dumpCount = orderTransferMysqlDAO.dumpCount(startTime, endTime);
		
		return createDumpRecord(0, Constance.DumpRecordType.TRANSFER, date);
	}

	private DumpRecordDO createB2cDumpRecord(Date date) {
//		String startTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 00:00:00";
//		String endTime = DateUtil.formatDate(date, DateUtil.SHORT_DATE_FORMAT_STR) + " 23:59:59";
//		Integer dumpCount = orderB2cMysqlDAO.dumpCount(startTime, endTime);
		
		return createDumpRecord(0, Constance.DumpRecordType.B2C, date);
	}
	
	

	private DumpRecordDO createDumpRecord(Integer dumpCount, Constance.DumpRecordType type, Date date) {
		
		DumpRecordDO dumpRecordDO = new DumpRecordDO();
		dumpRecordDO.setTotalCount((long)dumpCount);
		dumpRecordDO.setType(type.getCode());
		dumpRecordDO.setDay(date);
		dumpRecordDO.setDeleteState(0);
		dumpRecordDO.setSucceedCount(0L);
		dumpRecordDO.setFailedCount(0L);
		return dumpRecordDO;
	}

	private Date getBeforeDate(Date date, int beforeDay) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, - beforeDay);

		return c.getTime();
	}
}
